<?php

class Alump_BaseController {
	
	public $options = null;
	
	protected $_data = null;
	protected $_type = null;
	
	public static $PARAMS_COUNT = 0;
	
	
	public function __construct($type){
		$this->options = ALump::$options;
		$this->_type = $type;
	}
	
	public function getTheme($script){
		return __ROOT_DIR__.__THEME_DIR__.'/'.$this->options->theme.'/'.$script;
	}
	
	public function need($script){
		include $this->getTheme($script);
	}
		
	/**
	 * 文档标题
	 * @param unknown_type $cross
	 * @param unknown_type $p2
	 * @param unknown_type $sp
	 */
	public function archiveTitle($cross, $p2, $sp){
		//' &raquo; ', '', ' - '
		echo "";
	}
	
	public function header(){
		echo '
		<meta name="description" content="'.$this->options->description.'" />
		<meta name="keywords" content="'.$this->options->keywords.'" />
		<meta name="generator" content="'.ALump_Common::$VERSION_INFO.'" />
		<meta name="template" content="'.$this->options->theme.'" />
		<link rel="pingback" href="'.ALump_Common::url("/action/xmlrpc", $this->options->siteUrl).'" />
		<link rel="EditURI" type="application/rsd+xml" title="RSD" href="'.ALump_Common::url("/action/xmlrpc?rsd", $this->options->siteUrl).'" />
		<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="'.ALump_Common::url("/action/xmlrpc?wlw", $this->options->siteUrl).'" />
		<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="'.ALump_Common::url("/feed/", $this->options->siteUrl).'" />
		<link rel="alternate" type="application/rdf+xml" title="RSS 1.0" href="'.ALump_Common::url("/feed/rss/", $this->options->siteUrl).'" />
		<link rel="alternate" type="application/atom+xml" title="ATOM 1.0" href="'.ALump_Common::url("/feed/atom/", $this->options->siteUrl).'" />';
	}
	
	public function is($whois){

		
		switch($whois){
			case "index":
				if(empty($this->_type) || $this->_type == "page"){
					return true;
				}
		}
	}
	
	public function alump($alump){
		return ALump::Lump($alump);
	}
	
	public function _getPageNavUrl($pageno){
		$pageNav = $this->_data->pageNav;
		if($pageno <= 0){
			$pageno = 1;
		}
		if($pageno > $pageNav->pagecount){
			$pageno = $pageNav->pagecount;
		}
		if($this->is("index")){
			return ALump_Common::url("/page/$pageno", $this->options->siteUrl);
		}
	}
	
	protected  function _pageNav($prev = '&laquo;', $next = '&raquo;', $splitPage = 3, $splitWord = '...', $pageno){
		// 如果当前页前面的页数大于等于2*$splitWord，前面开始分割，前分割页比当前页小$splitPage，
		// 总页数大于等于2*$splitWord，开始后分割，后分割页比当前页大$splitPage，前后分割都只显示一页。
		
		
		$pageNav = $this->_data->pageNav;
		
		$pageNav->setPageno($pageno);
		$curPage = $pageNav->pageno;
		$double_splitPage = 2 * $splitPage;
		
		$prev_end = 0;
		
		$pageArr = array();
		array_push($pageArr, '<li><a class="prev" href="'.$this->_getPageNavUrl( $pageNav->pageno - 1).'">'.$prev.'</a></li>');
		if($curPage > $double_splitPage){ //开始前分割
			array_push($pageArr, '<li><a class="prev" href="'.$this->_getPageNavUrl(1).'">1</a></li>');
			array_push($pageArr, '<li>'.$splitWord.'</li>');
			$start = $curPage - $splitPage - 1;
			$start = $start > 0?$start:1;
			$prev_end = $double_splitPage+1;
			for($i = $start; $i <= $prev_end; $i++){
				array_push($pageArr, '<li><a class="prev" href="'.$this->_getPageNavUrl($i).'">'.$i.'</a></li>');
			}
		}else{
			$prev_end = $pageNav->pagecount >= $splitPage ? ($splitPage + 1 + $curPage):$pageNav->pagecount;
			for($i = 1; $i <= $prev_end; $i++){
				array_push($pageArr, '<li><a class="prev" href="'.$this->_getPageNavUrl($i).'">'.$i.'</a></li>');
			}
			
		}
		
		echo $prev_end;
		
		if($prev_end < $pageNav->pagecount - 1){
			$end_split = $double_splitPage + 2;
			if($pageNav->pagecount >= $end_split && ($curPage + $splitPage)  < $pageNav->pagecount){//开始后切割
				if($curPage > $double_splitPage){
			
					$start = $curPage + $splitPage + 1;
					$start = $start > $pageNav->pagecount?$pageNav->pagecount:$start;
					$end = $start + $splitPage;
					$end = $end > $pageNav->pagecount?$pageNav->pagecount:$end;
			
					for($i = $start; $i <= $end; $i++){
						array_push($pageArr, '<li><a class="prev" href="'.$this->_getPageNavUrl($i).'">'.$i.'</a></li>');
					}
				}
			
				array_push($pageArr, '<li>'.$splitWord.'</li>');
				array_push($pageArr, '<li><a class="prev" href="'.$this->_getPageNavUrl($pageNav->pagecount).'">'.$pageNav->pagecount.'</a></li>');
			
			}else{
				for($i = $curPage+1; $i <= $pageNav->pagecount; $i++){
					array_push($pageArr, '<li><a class="prev" href="'.$this->_getPageNavUrl($i).'">'.$i.'</a></li>');
				}
			}
		}

	
		
		
		
		$pageArr[$curPage] = '<li class="current"><a class="prev" href="'.$this->_getPageNavUrl($curPage).'">'.$curPage.'</a></li>';
		
		array_push($pageArr, '<li><a class="prev" href="'.$this->_getPageNavUrl($pageNav->pageno + 1).'">'.$next.'</a></li>');
		
		
		echo '<ol class="page-navigator">';
		echo implode("", $pageArr);
		echo '</ol>';
	}
	
	
}

?>